<template>
  <div class="tale-list-bottom">
    <Skeleton :count="15" v-if="!wordStore.list"></Skeleton>
    <el-table :data="tableData" stripe style="width: 100%" v-else>
      <el-table-column align="center" type="index" label="序号" width="180" />
      <el-table-column align="center" prop="username" label="用户名" width="180" />
      <el-table-column align="center" prop="text" label="文章内容" :show-overflow-tooltip="true">
        <template #default="{ row }">
          <div v-html="row.text"></div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="updatetime" label="更新日期">
        <template #default="{ row }">
          {{ formateDate(row.updatetime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button text type="primary" icon="Edit" size="small" @click="handleUpdate(row)"
            >编辑</el-button
          >
          <el-button text type="danger" icon="Delete" size="small" @click="handleDelete(row._id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <footer v-if="wordStore.list">
      <el-pagination
        v-model:current-page="page"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :small="true"
        layout=" sizes, prev, pager, next, jumper,total"
        :total="totalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </footer>
  </div>
</template>

<script setup lang="ts">
import { onMounted, computed, ref } from 'vue'
import { useWordStore } from '@/store/wordStore'
import { formateDate } from '@/utils/formatTime'
import { useRouter } from 'vue-router'
const router = useRouter()
let props = defineProps(['searchData'])
const wordStore = useWordStore()

const tableData = computed(() => {
  return wordStore.list
})

const totalCount = computed(() => {
  return wordStore.total
})

const page = ref(1)
const pageSize = ref(5) //测试为1，实际5

const fetchUserList = async (data: any = {}) => {
  try {
    let config = {
      page: page.value,
      pageSize: pageSize.value
    }
    config = { ...config, ...data }
    console.log('config', config)

    let res = await wordStore.getListAction('/tale/talelist', config)
    if (res?.code === -1) {
      // @ts-ignore
      ElMessage({
        type: 'error',
        message: res?.message
      })
      return
    }
    // dataList.value = userStore.userList as UserListResOfDataArr
    // totalCount.value = userStore.userListTotal as number
    // console.log(dataList.value)
  } catch (error) {}
}

// 处理分页器回调
const handleSizeChange = () => {
  fetchUserList(props.searchData)
}
const handleCurrentChange = () => {
  fetchUserList(props.searchData)
}

// 删除用户
const handleDelete = (id: string) => {
  wordStore.deleteListItem('/tale/talelist', id)
}

// 编辑按钮
const handleUpdate = (val: any) => {
  router.push({
    path: '/main/tale/stories',
    query: {
      textid: val._id,
      text: val.text
    }
  })
}

onMounted(() => {
  fetchUserList()
})

defineExpose({
  fetchUserList
})
</script>

<style lang="scss" scoped>
footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 15px;
}
</style>
